package com.eorchis.module.classapplydep.ui.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.core.service.IBaseService;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.commond.converter.IQueryCommondAttributeConverter;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.classapplydep.domain.ClassApplyDep;
import com.eorchis.module.classapplydep.domain.ClassApplyDepBean;
import com.eorchis.module.classapplydep.service.IClassApplyDepService;
import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepQueryCommond;
import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: ClassApplyDepController<br>
 * Description: 培训班-单位关系管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author HuangTF
 * @createDate 2013-12-18
 * @version $Revision$
 */
@Controller("classApplyDepController")
@RequestMapping(value = ClassApplyDepController.MODULE_PATH)
public class ClassApplyDepController extends AbstractBaseController<ClassApplyDepValidCommond,ClassApplyDepQueryCommond> {
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/classapplydep";
	// 培训班-单位关系接口
	@Resource(name = "main.java.com.eorchis.module.classapplydep.service.impl.ClassApplyDepServiceImpl")
	private IClassApplyDepService classApplyDepService;
	
	//用于排序
	@Autowired
	@Qualifier("attributeConverter")
	private ArrayList<IQueryCommondAttributeConverter> attributeConverter;
	
	@Override
	public IBaseService getService() {
		return classApplyDepService;
	}

	@Override
	public String getModulePath() {
		return MODULE_PATH;
	}
	/**
	 * 用于排序的方法
	 * @param queryCommond
	 * @param request
	 * @param attributeConverter
	 */
	@SuppressWarnings("unchecked")
	private void parameterValueConver(ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request,
			List<IQueryCommondAttributeConverter> attributeConverter) {
		for (IQueryCommondAttributeConverter queryCommondAttributeConverter : attributeConverter) {
			queryCommondAttributeConverter.convert(queryCommond,
					request.getParameterMap());
		}
	}
	/**
	 * 查询已选单位列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-18
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/findSelectedDepList")
	public String findSelectedDepList(
			@ModelAttribute("resultList") ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		if (attributeConverter != null) {
			parameterValueConver(queryCommond, request, attributeConverter);
		}
		List<ClassApplyDep> resultList=(List<ClassApplyDep>) classApplyDepService.findList(queryCommond);
		queryCommond.setResultList(resultList);
		return "";
	}
	/**
	 * 查询未选单位列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-18
	 */
	@RequestMapping(value = "/findNoSelectedDepList")
	public String findNoSelectedDepList(
			@ModelAttribute("resultList") ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		List<ClassApplyDepBean> resultList=classApplyDepService.findNoSelectedDepList(queryCommond);
		queryCommond.setResultList(resultList);
		return "";
	}
	
	/**
	 * 删除单位
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	@RequestMapping(value = "/deleteDepByIds")
	public String deleteDepByIds(@ModelAttribute("result") ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
					throws Exception
	{
		resultState.setState(ResultState.RESULT_STATE_FAIL);//默认状态失败
		if(PropertyUtil.objectNotEmpty(queryCommond.getSearchClassApplydepIds())&&queryCommond.getSearchClassApplydepIds().length>0)
		{
			classApplyDepService.delete(queryCommond.getSearchClassApplydepIds());
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage("删除成功");
		}
		else
		{
			resultState.setMessage("未选中任何单位");
		}
		return "";
	}
	/**
	 * 批量添加指定单位
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	@RequestMapping(value = "/saveApplyDep")
	public String saveApplyDep(@ModelAttribute("result") ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)throws Exception
	{
		if(PropertyUtil.objectNotEmpty(queryCommond.getDepIds()))
		{
			String mess=classApplyDepService.saveApplyDep(queryCommond);
			if(mess.equals("添加成功"))
			{
				resultState.setState(ResultState.RESULT_STATE_SUCCESS);
				resultState.setMessage(mess);
			}
			else
			{
				resultState.setState(ResultState.RESULT_STATE_FAIL);
				resultState.setMessage(mess);
			}
		}
		else
		{
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			resultState.setMessage("未选中任何单位");
		}
		return "";
	}
	/**
	 * 更新已选单位限制人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-19
	 */
	@RequestMapping(value = "/updatePersonNum")
	public String updatePersonNum(@ModelAttribute("result") ClassApplyDepQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)throws Exception
	{
		String mess=classApplyDepService.updatePersonNum(queryCommond);
		
		if(mess.equals("更改成功"))
		{
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
			resultState.setMessage(mess);
		}
		else
		{
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			resultState.setMessage(mess);
		}
		return "";
	}
}
